package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {


    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);


        // 在菜品表 插入数据 一条
        dishMapper.insert(dish);
        // 获取insert语句生成的主键值
        Long id = dish.getId();
        // 在口味表 插入数据  0条或多条
        List<DishFlavor> flavors = dishDTO.getFlavors();


        if(flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor ->{
                    dishFlavor.setDishId(id);
            });

            dishFlavorMapper.insertBatch(flavors);

        }

    }


    public PageResult pageService(DishPageQueryDTO dishPageQueryDTO) {

        // 使用pagehlper 分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }


    public void deleteBatch(List<Long> ids) {

        // 1.起售中的 不能删除
        // 2.关联了套餐的不能删除
        // 3.如果可以删除，删除菜品  同时需要把dishflavor中的 口味也删除

        // 1.
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            //判断每一个dish 是否是起售中 ，如果是 则抛出异常
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 2.
        List<Long> semealids = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(semealids!=null && semealids.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 3.
        for (Long id : ids) {
            dishMapper.deleteById(id);
            // 删除菜品关联的口味
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    public DishVO getByIdWithFlavor(Long id) {

        // 根据id查询菜品信息
        // 根据id查询口味数据
        // 数据封装 到 dishvo


        Dish dish = dishMapper.getById(id);
        List<DishFlavor> dishFlavors=  dishFlavorMapper.getByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }


    public void updateWithFlavor(DishDTO dishDTO) {

        // 修改菜品表
        // 2 修改口味表 2.1先把原先的口味删了 2.2 再把新提交的增加上去

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        // 2.1
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        // 2.2
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insertBatch(flavors);
        }

    }



    public List<Dish> list(Long categoryId) {

        Dish dish = Dish.builder().categoryId(categoryId).status(StatusConstant.ENABLE).build();
        List<Dish> list = dishMapper.list(dish);


        return list;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;

    }

}
